﻿using System.Data.Entity;

namespace Anmlz.Data
{
    using System.Data.Entity.Infrastructure;

    using Anmlz.Model.Entities;
    using Anmlz.Services;

    public class MigrationsContextFactory : IDbContextFactory<AnmlzDbContext>
    {
        public AnmlzDbContext Create()
        {
            return new AnmlzDbContext();
        }
    }

    public class AnmlzDbContext : DbContext
    {
        public AnmlzDbContext() : base("Anmlz")
        {
          //  Database.SetInitializer<AnmlzDbContext>(anmlzDatabaseInitializer);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            Configuration.ProxyCreationEnabled = false;
            Configuration.LazyLoadingEnabled = false;

            modelBuilder.Entity<Creature>().HasRequired(c => c.CreatureType).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<Creature>().HasRequired(c => c.CreatureColor).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<Creature>().HasRequired(c => c.CreatureSizeType).WithMany().WillCascadeOnDelete(false);

            modelBuilder.Entity<Breed>().HasRequired(c => c.CreatureType).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<Breed>().HasRequired(c => c.CreatureSizeType).WithMany().WillCascadeOnDelete(false);

            modelBuilder.Entity<User>().HasRequired(c => c.Country).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<User>().HasRequired(c => c.Avatar).WithMany().WillCascadeOnDelete(false);

            modelBuilder.Entity<City>().HasRequired(c => c.Country).WithMany().WillCascadeOnDelete(false);


            modelBuilder.Entity<ImageToUser>().HasRequired(c => c.Image).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<Creature>().HasRequired(c => c.Avatar).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<ImageToCreature>().HasRequired(c => c.Image).WithMany().WillCascadeOnDelete(false);

            modelBuilder.Entity<SellingAd>().HasRequired(c => c.BreedGroup).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<SellingAd>().HasRequired(c => c.CreatureType).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<SellingAd>().HasRequired(c => c.CreatureSizeType).WithMany().WillCascadeOnDelete(false);

            modelBuilder.Entity<CreatureTemperament>().HasRequired(c => c.CreatureType).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<CreatureColor>().HasRequired(c => c.CreatureType).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<CreatureCharacteristic>().HasRequired(c => c.CreatureType).WithMany().WillCascadeOnDelete(false);

            modelBuilder.Entity<Breed>().HasOptional(c => c.BreedAdultImage).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<Breed>().HasOptional(c => c.BreedPetImage).WithMany().WillCascadeOnDelete(false);
        }

        public DbSet<UserType> UserTypes { get; set; }
        public DbSet<BreedGroup> BreedGroups { get; set; }
        public DbSet<Breed> Breeds { get; set; }
        public DbSet<BreedToCreatureTemperament> BreedToCreatureTemperaments { get; set; }
        public DbSet<BreedToCreatureColor> BreedToCreatureColors { get; set; }
        public DbSet<BreedToCreatureCharacteristic> BreedToCreatureCharacteristics { get; set; }
        public DbSet<City> Cities{ get; set; }
        public DbSet<Country> Countries{ get; set; }
        public DbSet<Creature> Creatures{ get; set; }
        public DbSet<CreatureColor> CreatureColors { get; set; }
        public DbSet<CreatureSizeType> CreatureSizeTypes { get; set; }
        public DbSet<CreatureType> CreatureTypes { get; set; }
        public DbSet<CreatureTemperament> CreatureTemperaments { get; set; }
        public DbSet<CreatureCharacteristic> CreatureCharacteristics { get; set; }
        public DbSet<UserRole> UserRoles{ get; set; }
        public DbSet<User> Users{ get; set; }
        public DbSet<UserInRole> UserInRoles{ get; set; }
        public DbSet<UserWebsiteUrl> UserWebsiteUrls{ get; set; }
        public DbSet<Image> Images { get; set; }
        public DbSet<Currency> Currencies { get; set; }
        public DbSet<ImageToUser> ImageToUsers { get; set; }
        public DbSet<ImageToCreature> ImageToCreatures { get; set; }
        public DbSet<SellingAd> SellingAds { get; set; }
        public DbSet<ImageToSellingAd> ImageToSellingAds { get; set; }
    }
}